var layer = layui.layer //弹层
    ,table = layui.table //表格
form = layui.form,$ = layui.jquery,laydate = layui.laydate;
lay('.starttime').each(function(){
    laydate.render({
        elem: this
    });
});
function getAllJson(jsons) {
    let tmpitem=[]
    for (let item of jsons) {
        let tmparr={}
        tmparr["value"]=item["code"];
        tmparr["name"]=item["name"];
        if(item["users"]){
            for(let it of item["users"]){
                item["list"].push(it)
            }
        }
        if(item["list"] && item["list"].length){
            tmparr["children"]=   this.getAllJson(item["list"]);
            tmpitem.push(tmparr)
        }else{
            tmpitem.push(tmparr)
        }

    }
    return tmpitem
}

function getxmselect(el,name, data) {
    return xmSelect.render({
        el: el,
        name: name,
        tree: {
            show: true,
            showLine: true,
            showFolderIcon:true,
            indent: 20,
            expandedKeys:false
        },
        height: 'auto',
        data: data,
        on: function ({ arr, change, isAdd }) {
            return this
        }
    })
}


//select over
(function () {
    let operationList = getCurrentDepartmentUser();
    if(operationList && operationList.list){
        let operationTree = getAllJson(operationList.list);
        getxmselect("#operationCode","operationCode", operationTree)
    }else {
        getxmselect("#operationCode","operationCode", [{name: user.name, value: user.code}])
    }
})()

let loginUserCode;
layer.ready(function () {
    // //获取当前登陆用户名称
    // $.ajax({
    //     url:"cost/getLoginUserInfo",
    //     async:false,
    //     success:function(data){
    //         if(data.status == 200){
    //             loginUserCode=data.data.code;
    //             //当前登录人所管理的员工:操作员
    //             let manageOperatorRulCost=getManageRule("costOperator");
    //             for(let i in manageOperatorRulCost){
    //                 $("#operationNo").append("<option value=\"" + manageOperatorRulCost[i].code + "\">" + manageOperatorRulCost[i].name + "</option>");
    //             }
    //             form.render('select');
    //         }
    //     }
    // });
});
table.render({
    elem: '#mains'
    ,url:'cost/getResourceSamplesListByPage'
    , where: {
        operationCode:user.code ,
    }
    ,cols: [[
        {checkbox: true, sort : true,title:'选择'}
        ,{field:'billNo', title:'单据编号',sort:true}
        ,{field: 'createDate', title: '创建日期', sort: true}
        ,{field:'customerName', title:'客户名称',sort:true}
        ,{field: 'customerCode', hide:true}
        ,{field:'operator', title:'操作员',sort:true}
        ,{field:'operationNo', hide:true}
        ,{field:'amountSubtotal', title:'金额小计',sort:true}
    ]]
    ,id: 'mainstableid'
    ,height: 500
    ,page: true
});
function req(data){
    let req=data.field;
    let op=req.operationNo;
    if (isEmpty(op)){
        req.operationNo=loginUserCode
    }
    return req
}
//字段是否为空或者零
function isEmpty(obj){
    if(typeof obj == "undefined" || obj == null || obj == "" || obj == 0){
        return true;
    }else{
        return false;
    }
}
form.on('submit(submitform)', function(data){
    table.reload('mainstableid', {
        page: {
            curr: 1 //重新从第 1 页开始
        }
        ,where: req(data)
    }, 'data');
    return false;
});

$("#choose").on("click",function () {
    var purchaseorderdata =   table.checkStatus("mainstableid").data;
    if(purchaseorderdata.length == 0){
        layer.msg("请选择数据")
    }else{
        var oldData =  parent.table.cache["maininformationtableid"];
        //循环添加子页面的数据到父页面，字段要一致，负责添加不上去
        $.each(purchaseorderdata, function (index, value) {
            purchaseorderdata[index]['applicationAmount']=purchaseorderdata[index].amountSubtotal;
            purchaseorderdata[index]['specialDesc']=purchaseorderdata[index].billNo;
            purchaseorderdata[index]['salesmanName']=purchaseorderdata[index].operator;
            purchaseorderdata[index]['salesmanCode']=purchaseorderdata[index].operationNo;
            purchaseorderdata[index]['tempId']=Number(Math.random().toString().substr(3,3) + Date.now()).toString(36);
            purchaseorderdata[index]['billDate']=purchaseorderdata[index].createDateStr;
            purchaseorderdata[index]['increaseCode'] = "无";

            oldData.push(purchaseorderdata[index]);
        });
        parent.table.reload('maininformationtableid', {data:oldData, limit:oldData.length}); //刷新主信息表格
        //执行接口
        layer.msg("保存成功", {icon: 6});
        setTimeout(function () {
            //保存成功则关闭此页面
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        },1000)
    }
})
$("#shutdown").on("click",function () {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
})